package design.onekey.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import design.onekey.base.BaseEntity;
import design.onekey.constant.enums.DelFlagEnum;
import design.onekey.base.PageEntity;
import design.onekey.mapper.UserRoleMenuMapper;
import design.onekey.model.UserRoleMenu;
import design.onekey.service.IUserRoleMenuService;
import design.onekey.util.MessageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @Description: UserRoleMenu服务实现
* @author
* @date 2020-06-05 04:03:46
* @version V1.0
*/
@Service
public class UserRoleMenuServiceImpl extends ServiceImpl<UserRoleMenuMapper, UserRoleMenu> implements IUserRoleMenuService {

   /**
   * @Title: tablePagingQuery
   * @Description: 表格分页查询
   * @param @param model
   * @return BaseEntity
   * @author
   * @throws
   */
   @Override
   public PageEntity tablePagingQuery(UserRoleMenu model) {
       PageEntity result = new PageEntity();
       model.setDelFlag(DelFlagEnum.UNDEL.getCode());
       com.github.pagehelper.Page<?> page = PageHelper.startPage(model.getCurrentPage(), model.getPageSize()); //分页条件
       List<UserRoleMenu> list = this.baseMapper.searchDataLike(model);
       result.setData(list);
       result.setTotal(page.getTotal());
       result.setCurrentPage(model.getCurrentPage());
       result.setPageSize(model.getPageSize());
       return result;
   }


   /**
   *<p>Title: getDetail</p>
   *<p>Description: 获取详情</p>
   * @author
   * @param id
   * @return
   */
   @Override
   public BaseEntity getDetail(Long id) {
       return this.getById(id);
   }

   /**
   *<p>Title: insert</p>
   *<p>Description: 新增数据</p>
   * @author
   * @param model
   * @return
   */
   @Transactional
   @Override
   public BaseEntity insert(UserRoleMenu model) {
       BaseEntity result = new BaseEntity();
       this.save(model);
       result.setStatusMsg(MessageUtil.getValue("info.insert"));
       return result;
   }

   /**
   *<p>Title: update</p>
   *<p>Description: 更新数据</p>
   * @author
   * @param model
   * @return
   */
   @Transactional
   @Override
   public BaseEntity update(UserRoleMenu model) {
       BaseEntity result = new BaseEntity();
       this.updateById(model);
       result.setStatusMsg(MessageUtil.getValue("info.update"));
       return result;
   }

   /**
   *<p>Title: delete</p>
   *<p>Description: 单条删除数据</p>
   * @author
   * @param model
   * @return
   */
   @Transactional
   @Override
   public BaseEntity delete(Long id) {
       UserRoleMenu userRoleMenu = new UserRoleMenu();
       userRoleMenu.setId(id);
       userRoleMenu.setDelFlag(DelFlagEnum.DEL.getCode());
       this.updateById(userRoleMenu);
       BaseEntity result = new BaseEntity();
       result.setStatusMsg(MessageUtil.getValue("info.delete"));
       return result;
   }

   /**
   *<p>Title: deleteBatch</p>
   *<p>Description: 批量删除数据</p>
   * @author
   * @param list
   * @return
   */
   @Transactional
   @Override
   public BaseEntity deleteBatch(List<Long> ids) {
       List<UserRoleMenu> list = new ArrayList<UserRoleMenu>();
       for (int i = 0; i < ids.size(); i++) {
           UserRoleMenu userRoleMenu = new UserRoleMenu();
           userRoleMenu.setId(ids.get(i));
           userRoleMenu.setDelFlag(DelFlagEnum.DEL.getCode());
           list.add(userRoleMenu);
       }
       BaseEntity result = new BaseEntity();
       if (list != null && list.size() > 0) {
           this.updateBatchById(list);
       }
       result.setStatusMsg(MessageUtil.getValue("info.delete"));
       return result;
   }
}
